<template>
  <div class="container">
    <div style="padding: 10px 20px;">
      <NormalMaterial :currentTab="currentTab" :tabList="tabList" :formList="formList" :queryForm="queryForm" pagePwd="Form" @tabClick="tabClick" @handleQuery="handleQuery" @handleReset="handleReset">
        <template slot="one">
          <el-table v-loading="loading" :data="companyTableList" style="width: 100%" height="400" :header-cell-style="headerCellStyle" :row-style="rowStyle" :cell-style="cellStyle">
            <el-table-column type="index" width="55" align="center" label="序号"/>
            <el-table-column label="链接标题" prop="title">
              <template slot-scope="{ row }">
                <div style="color: #082aff;cursor: pointer;" @click="edit(row)">
                  {{ row.title }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="标签" prop="label">
              <template slot-scope="scope">
                <tagGroupShowTable :tagGroup="scope.row.weGroupTagVOS" />
              </template>
            </el-table-column>
            <el-table-column label="填写人数" prop="writeCount">
              <template slot-scope="scope">
                所有填写：{{ scope.row.writeCount }}
              </template>
            </el-table-column>
            <el-table-column label="创建人" prop="createBy"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="180" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <el-button type="text" @click="edit(row)" v-hasPermi="['wecome:material:edit:corp']">配置详情</el-button>
                <el-button type="text" @click="result(row)" v-hasPermi="['tableManage:result']">填写结果</el-button>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="companyPageNum" :pageSize="companyPageSize" :total="companyTotal"
            @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
        </template>
        <template slot="two">
          <div style="text-align: right; padding: 20px 0; border-top: 1px solid #eee;">
            <el-button icon="el-icon-plus" type="primary" @click="toArticle()" v-has-permi="['admin:linkmanage:addLinkMaterial']">创建问卷</el-button>
          </div>
          <el-table v-loading="loading" :data="selfTableList" style="width: 100%" height="400" :header-cell-style="headerCellStyle" :row-style="rowStyle" :cell-style="cellStyle">
            <el-table-column type="index" width="55" align="center" label="序号"/>
            <el-table-column label="链接标题" prop="title">
              <template slot-scope="{ row }">
                <div style="color: #082aff;cursor: pointer;" @click="edit(row)">
                  {{ row.title }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="标签" prop="label">
              <template slot-scope="scope">
                <tagGroupShowTable :tagGroup="scope.row.weGroupTagVOS" />
              </template>
            </el-table-column>
            <el-table-column label="填写人数" prop="writeCount">
              <template slot-scope="scope">
                所有填写：{{ scope.row.writeCount }}
              </template>
            </el-table-column>
            <el-table-column label="创建人" prop="createBy"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="180" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <el-button type="text" @click="edit(row)" v-hasPermi="['wecom:material:edit']">配置详情</el-button>
                <el-button type="text" @click="result(row)" v-hasPermi="['tableManage:Presult']">填写结果</el-button>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="selfPageNum" :pageSize="selfPageSize" :total="selfTotal"
            @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange">
          </NormalPagination>
        </template>
      </NormalMaterial>
    </div>
  </div>
</template>

<script>
import { getTableManageList } from '@/api/article/group'
import { dateFormat } from '@/utils/index'

export default {
  data() {
    return {
      categoryId: null,
      routerPath: '',
      tabList: [
        { label: '企业问卷库', vlaue: '0', slotName: 'one' },
        { label: '我上传的问卷', vlaue: '1', slotName: 'two' },
      ],
      queryForm: {
        title: '',
        deptId: null,
        createBy: '',
      },
      companyForm: {
        title: '',
        deptId: null,
        createBy: '',
      },
      selfForm: {
        title: '',
        deptId: null,
        createBy: '',
      },
      formList: [
        {
          label: '问卷名称',
          value: '',
          prop: 'title',
          type: 'INPUT',
          emunName: '',
        },
        {
          label: '管理机构',
          value: null,
          prop: 'deptId',
          type: 'MANAGE_COM',
          emunName: '',
        },
        {
          label: '创建人',
          value: '',
          prop: 'createBy',
          type: 'STAFF',
          emunName: '',
        },
      ],
      currentTab: '0',
      loading: false,
      companyTableList: [],
      companyTotal: 0,
      companyPageNum: 1,
      companyPageSize: 20,
      selfTableList: [],
      selfTotal: 0,
      selfPageNum: 1,
      selfPageSize: 20,
      headerCellStyle: {
        background: '#F8F8F8',
        color: '#1D1D1D',
        height: '40px',
        textAlign: 'center',
        borderLeft: 'none',
        borderRight: 'none',
      },
      rowStyle: {
        height: '40px',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
      },
      cellStyle: {
        height: '40px',
        padding: '0',
        textAlign: 'center',
        borderLeft: 'none',
        borderRight: 'none',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
      },
    }
  },
  created() {
    this.routerPath = window.globalConfig.SHARE_URL + '/linkForm?id='
    this.currentTab = this.$route.query.is_person ? this.$route.query.is_person + '' : '0'
    this.handleQuery()
  },
  methods: {
    tabClick(val) {
      this.categoryId = null
      this.currentTab = val
      this.handleReset()
      // if (this.currentTab == '0') {
      //   this.queryForm = this.companyForm
      // } else {
      //   this.queryForm = this.selfForm
      // }
      // this.handleQuery(this.categoryId, 1)
    },
    handleQuery(categoryId, pageNum) {
      this.loading = true
      this.categoryId = categoryId
      let tempNum = this.currentTab == '0' ? this.companyPageNum : this.selfPageNum
      let tempSize = this.currentTab == '0' ? this.companyPageSize : this.selfPageSize
      let params = {
        pageNum: pageNum || tempNum,
        pageSize: tempSize,
        mediaType: 10,
        levelType: +this.currentTab,
        groupId: this.categoryId,
        title: this.queryForm.title,
        deptId: this.queryForm.deptId,
        createBy: this.queryForm.createBy ? this.queryForm.createBy : null,
      }
      getTableManageList(params).then((res) => {
        this.loading = false
        if (res.code === 200) {
          let { rows, total } = res
          rows.forEach((item) => {
            item.createTime = dateFormat(item.createTime, 'yyyy-MM-dd hh:mm:ss')
          })
          if (this.currentTab == '0') {
            this.companyForm = this.queryForm
            this.companyTableList = rows || []
            this.companyTotal = total || 0
          } else {
            this.selfForm = this.queryForm
            this.selfTableList = rows || []
            this.selfTotal = total || 0
          }
        }
      })
    },
    handleReset() {
      this.queryForm = {
        title: '',
        deptId: null,
        createBy: ''
      }
      if (this.currentTab == '0') {
        this.companyForm = this.queryForm
      } else {
        this.selfForm = this.queryForm
      }
      this.handleQuery(this.categoryId, 1)
    },
    handleSizeChange(val) {
      if (this.currentTab == '0') {
        this.companyPageNum = 1
        this.companyPageSize = val
      } else {
        this.selfPageNum = 1
        this.selfPageSize = val
      }
      this.handleQuery(this.categoryId)
    },
    handleCurrentChange(val) {
      if (this.currentTab == '0') {
        this.companyPageNum = val
      } else {
        this.selfPageNum = val
      }
      this.handleQuery(this.categoryId)
    },
    edit(row) {
      this.$router.push({
        name: 'FormManage/details',
        query: {
          id: row.id,
          levelType: this.currentTab,
          is_person: +this.currentTab
        }
      })
    },
    toEdit(row) {
      this.$router.push({
        name: 'FormManage/edit',
        query: {
          id: row.id,
          levelType: this.currentTab,
          is_person: +this.currentTab
        }
      })
    },
    result(row) {
      console.log(row)
      this.$router.push({
        name: 'FormManage/result',
        query: { id: row.id, is_person: +this.currentTab }
      })
    },
    toArticle(name) {
      // this.$router.push('/customerOperations/article/edit?name='+name)
      this.$router.push({
        name: 'FormManage/edit',
        query: { levelType: this.currentTab, is_person: +this.currentTab }
      })
    },
    toInfo(row) {
      this.$router.push({
        name: 'Article/Info',
        query: { id: row.id, is_person: +this.currentTab }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/base.scss";

.container {
  background: #fff;

  ::v-deep {
    .el-tabs__header {
      margin: 0;
    }

    .el-tree {
      padding: 10px 10px 0 10px;
    }

    .el-tree-node__content {
      height: 35px;
      padding-left: 10px !important;
    }
  }

}
.container {
  height: 100%;
  background: #fff;
}
</style>
